package it.univaq.mwt.frontoffice.action;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import it.mwt.uivaq.utility.MailUtility;
import it.univaq.mwt.business.UserService;
import it.univaq.mwt.common.business.UsersServiceFactory;
import it.univaq.mwt.frontoffice.form.MailForm;
import it.univaq.mwt.log.LivelloLog;
import it.univaq.mwt.log.LogManager;
import it.univaq.mwt.model.entity.Users;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;

public class ManageMailAction extends MappingDispatchAction implements
		LivelloLog {

	public ActionForward showMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		
		

		int id = 0;
		if (session.getAttribute("id") != null) {

			UsersServiceFactory userFactory = UsersServiceFactory.getInstance();
			UserService facadeUser = userFactory.getUserService();
			
			Users user=facadeUser.getUser(id);
			
			request.setAttribute("user", user);

		}

		return mapping.findForward("success");
	}

	public ActionForward sendMail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		HttpSession session = request.getSession();
		
		MailForm mailForm=(MailForm)form;
		
		 String mitt="g_buzzelli@yahoo.it";
		 String dest=mailForm.getDest();
		 String obj="";
		 String text=mailForm.getObj();
		 
		 try
		    {
		      MailUtility.sendMail(dest, mitt, obj, text);
		      request.setAttribute("message", "send success");
		    }
		    catch (MessagingException exc)
		    {
		    	LogManager.getIstance().scrivi("MessagingException:", ERR);
		    	request.setAttribute("message", "Send Failed!");
		    }
		

		    return mapping.findForward("success");
	}
}
